
import 'dart:developer';

import 'package:DiamondDiary/utils/route_helper.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import 'loading_widgets.dart';



class Dialogs{

  static void showAlert(BuildContext context,String content,{VoidCallback onConfirm}){
    showCupertinoDialog(context: context,
        builder:(context)=>CupertinoAlertDialog(
          content: Text(content,textAlign: TextAlign.center,style: TextStyle(fontSize: 14),),
          actions: [
            TextButton(onPressed:(){
              if(onConfirm!=null){
                onConfirm();
              }
              Navigator.pop(context);
            }, child: Text('确认'))
          ],
        )
    );
  }

  static void showTipAlert(BuildContext context,String content,{String title,String confrimText="确认",VoidCallback onConfirm,VoidCallback onCancel}){
    showCupertinoDialog(context: context,
        builder:(context)=>CupertinoAlertDialog(
          title: title==null?null:Text(title,textAlign: TextAlign.center,style: TextStyle(fontSize: 16,fontWeight: FontWeight.w600),),
          content: Text(content,textAlign: TextAlign.center,style: TextStyle(fontSize: 14),),
          actions: [
            TextButton(onPressed:(){
              Navigator.pop(context);
              if(onCancel!=null) onCancel;
            }, child: Text('取消')),
            TextButton(onPressed:(){
              Navigator.pop(context);
              if(onConfirm!=null) onConfirm();
            }, child: Text(confrimText))
          ],
        )
    );
  }



  static void showInputDialog(BuildContext context,String title,String init,TextEditingController controller,Function callback,
      {String hint='',int maxLines=1,int maxLength,TextInputType inputType}) {
    if(init?.isNotEmpty??false)//这里加了判断, cancel时候没有清除,可能存在切换后文字没有清除的情况
      controller.text = init;
    if(maxLines < 2) {
      maxLines = (inputType == TextInputType.number || inputType == TextInputType.phone)?1:3;
    }
    showCupertinoDialog(
        context: context,
        builder: (context) {
          return CupertinoAlertDialog(
            title: Text(title),
            content: Card(
              shape:RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(5))),
              child:TextField(
                maxLength: maxLength,
                maxLines: maxLines,
                autofocus: true,
                controller: controller,
                keyboardType: inputType,
                decoration: InputDecoration(
                  hintText: hint,
                  // filled: true,
                  // fillColor: Colors.grey.shade50,
                  border: InputBorder.none,
                  contentPadding: EdgeInsets.all(10),
                ),
              ),
            ),
            actions: <Widget>[
              CupertinoDialogAction(
                onPressed: () {
                  Navigator.pop(context);
                },
                child: Text('取消'),
              ),
              CupertinoDialogAction(
                onPressed: () {
                  callback(controller.text);
                  controller.text = '';
                  Navigator.pop(context);
                },
                child: Text('确定'),
              ),
            ],
          );
        });
  }

  static void showLoading(BuildContext context,{String content}){
    showDialog(context: context,
      barrierDismissible:false,
      builder: (context) => LoadingDialog(text: content??'加载中...',),
    );
  }

}

class LoadingDialog extends Dialog {

  static var SHOWING = false;

  String text;


  LoadingDialog({this.text,}) ;

  Future<bool> checkPop() async{
    SHOWING = false;
    return true;
  }

  //Alert: 必须使用dissmiss来清除dialog
  static void dissmiss(BuildContext context){
    if(SHOWING) {
      SHOWING = false;
      RouteHelper.pop(context);
    }
  }

  @override
  Widget build(BuildContext context) {
    SHOWING = true;
    return WillPopScope(onWillPop:() => checkPop(),
      child: Material(
      //创建透明层
      type: MaterialType.transparency, //透明类型
      child: Center(
        //保证控件居中效果
        child: SizedBox(
          width: 100.0,
          height: 100.0,
          child: Container(
            decoration: ShapeDecoration(
              color: Color(0xffffffff),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.all(
                  Radius.circular(8.0),
                ),
              ),
            ),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                LoadingWidget(),
                text!=null?
                Padding(
                  padding: const EdgeInsets.only(top: 20.0,),
                  child: new Text(text, style: new TextStyle(fontSize: 12.0,color:Colors.black87),),
                ):SizedBox(),
              ],
            ),
          ),
        ),
      ),),
    );
  }
}